import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'

const configureStore = (rootReducer, initialState = {}) => {
  const middlewares = [thunkMiddleware]
  const middlewareEnhancer = applyMiddleware(...middlewares)

  const enhancers = [
    middlewareEnhancer,
  ]

  if (ENV.IS_CLIENT && ENV.IS_DEV && typeof window.__REDUX_DEVTOOLS_EXTENSION__ !== 'undefined') {
    enhancers.push(window.__REDUX_DEVTOOLS_EXTENSION__())
  }

  const composedEnhancers = compose(...enhancers)

  const store = createStore(combineReducers(rootReducer), initialState, composedEnhancers)

  return store
}

export default configureStore